package com.example.demo.service.impl;

import com.example.demo.dto.ProcessConfigDTO;
import com.example.demo.dto.ProcessDetailDTO;
import com.example.demo.dto.ProcessNodeDTO;
import com.example.demo.entity.ProcessConfig;
import com.example.demo.mapper.ProcessConfigMapper;
import com.example.demo.request.CreateProcessConfigRequest;
import com.example.demo.service.ProcessConfigService;
import com.example.demo.service.ProcessNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProcessConfigServiceImpl implements ProcessConfigService {

    @Autowired
    private ProcessConfigMapper processConfigMapper;

    @Autowired
    private ProcessNodeService processNodeService;

    @Override
    public List<ProcessConfigDTO> getProcessConfigs(int page, int size) {
        // 计算偏移量
        int offset = page * size;
        // 查询分页数据
        List<ProcessConfig> processConfigs = processConfigMapper.selectPage(offset, size);
        return processConfigs.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    @Override
    public long countProcessConfigs() {
        // 统计总记录数
        return processConfigMapper.count();
    }

    @Override
    public ProcessDetailDTO getProcessDetail(Long id) {
        ProcessConfig processConfig = processConfigMapper.selectById(id);
        if (processConfig == null) {
            return null;
        }

        ProcessDetailDTO detailDTO = new ProcessDetailDTO();
        detailDTO.setId(processConfig.getId());
        detailDTO.setName(processConfig.getName());
        detailDTO.setDescription(processConfig.getDescription());
        detailDTO.setStatus(processConfig.getStatus());
        detailDTO.setVersion(processConfig.getVersion());

        // 查询该流程的所有节点
        List<ProcessNodeDTO> nodes = processNodeService.getProcessNodes(id);
        detailDTO.setNodes(nodes);

        return detailDTO;
    }

    @Override
    public ProcessConfigDTO createProcessConfig(CreateProcessConfigRequest request) {
        ProcessConfig processConfig = new ProcessConfig();
        processConfig.setName(request.getName());
        processConfig.setDescription(request.getDescription());
        processConfig.setStatus(request.getStatus());
        processConfig.setVersion(request.getVersion());
        processConfig.setCreatedAt(LocalDateTime.now());
        processConfig.setUpdatedAt(LocalDateTime.now());

        processConfigMapper.insert(processConfig);
        return convertToDTO(processConfig);
    }

    @Override
    public ProcessConfigDTO updateProcessConfig(Long id, CreateProcessConfigRequest request) {
        ProcessConfig processConfig = processConfigMapper.selectById(id);
        if (processConfig == null) {
            return null;
        }

        processConfig.setName(request.getName());
        processConfig.setDescription(request.getDescription());
        processConfig.setStatus(request.getStatus());
        processConfig.setVersion(request.getVersion());
        processConfig.setUpdatedAt(LocalDateTime.now());

        processConfigMapper.updateById(processConfig);
        return convertToDTO(processConfig);
    }

    @Override
    public void deleteProcessConfig(Long id) {
        processConfigMapper.deleteById(id);
        // 由于数据库设置了级联删除，节点会自动删除
    }

    private ProcessConfigDTO convertToDTO(ProcessConfig processConfig) {
        ProcessConfigDTO dto = new ProcessConfigDTO();
        dto.setId(processConfig.getId());
        dto.setName(processConfig.getName());
        dto.setDescription(processConfig.getDescription());
        dto.setStatus(processConfig.getStatus());
        dto.setVersion(processConfig.getVersion());
        dto.setCreatedAt(processConfig.getCreatedAt());
        dto.setUpdatedAt(processConfig.getUpdatedAt());
        return dto;
    }
}